package cn.chenwx.bbsforum.service.Impl;

import cn.chenwx.bbsforum.entity.Article;
import cn.chenwx.bbsforum.entity.Comment;
import cn.chenwx.bbsforum.mapper.CommentMapper;
import cn.chenwx.bbsforum.service.ArticleService;
import cn.chenwx.bbsforum.service.CommentService;
import cn.chenwx.bbsforum.service.UserService;
import cn.chenwx.bbsforum.utils.BlogJSONResult;
import cn.chenwx.bbsforum.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    @Override
    public int findCommentsByArticleId(int id) {
        return commentMapper.findCommentsByArticleId(id);
    }

    @Override
    public List<Comment> findContentByArticleId(int id) {
        List<Comment> comments=commentMapper.findAllByArticleId(id);
        for(Comment comment : comments){
            if (comment.getCreateTime()!=null){
                comment.setCreateTimeStr(DateUtils.dateToString(comment.getCreateTime(),"yyyy-MM-dd HH:mm"));
                comment.setAnswererName(userService.findUserById(comment.getAnswererId()).getUsername());
            }
        }
        return comments;
    }

    @Override
    public BlogJSONResult DeleteContentById(int id) {
         commentMapper.deleteContentById(id);
         return BlogJSONResult.ok();
    }

    @Override
    public List<Comment> getAllComment(int pageNum, int rows) {
        PageHelper.startPage(pageNum,rows);
        List<Comment> comments = commentMapper.getAllComment();
        for (Comment comment: comments){
            if (comment.getCreateTime()!=null){
                comment.setCreateTimeStr(DateUtils.dateToString(comment.getCreateTime(),"yyyy-MM-dd HH:mm"));
            }
        }
        return comments;
    }

    @Override
    public int getAllComments() {
        return commentMapper.getAllComments();
    }

    @Override
    public BlogJSONResult getUserComment(int rows, int pageNum, String username) {
        int userId = userService.findUserByName(username).getId();
        PageHelper.startPage(pageNum, rows);
        List<Comment> comments = commentMapper.getUserCommentByRespondentId(userId);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);

        JSONObject returnJson = new JSONObject();
        JSONObject commentJson;
        JSONArray commentJsonArray = new JSONArray();
        for(Comment comment : comments){
            commentJson = new JSONObject();
            commentJson.put("id",comment.getId());
            commentJson.put("articleId",comment.getArticleId());
            commentJson.put("answererId", comment.getId());
            commentJsonArray.add(commentJson);


        }
        returnJson.put("result",commentJsonArray);
        returnJson.put("msgIsNotReadNum",commentMapper.countIsReadNumByRespondentId(userId));

        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum",pageInfo.getPageNum());
        pageJson.put("pageSize",pageInfo.getPageSize());
        pageJson.put("total",pageInfo.getTotal());
        pageJson.put("pages",pageInfo.getPages());
        pageJson.put("isFirstPage",pageInfo.isIsFirstPage());
        pageJson.put("isLastPage",pageInfo.isIsLastPage());
        returnJson.put("pageInfo",pageJson);

        return BlogJSONResult.ok(returnJson);
    }

    public BlogJSONResult getUserComments(String username){
        int userId = userService.findUserByName(username).getId();

        return BlogJSONResult.ok( commentMapper.countIsReadNumByRespondentId(userId));
    }

    @Override
    public BlogJSONResult getComments(int id) {
        List<Comment> commentLists = commentMapper.findContentByArticleId(id);
        JSONArray commentJsonArray = new JSONArray();

        JSONObject commentJsonObject;

        for(Comment comment:commentLists){
            commentJsonObject = new JSONObject();
            //封装评论
            commentJsonObject.put("id",comment.getId());
            commentJsonObject.put("answererid",userService.findUserById(comment.getAnswererId()));
            commentJsonObject.put("commentDate",comment.getCreateTime());
//            commentJsonObject.put("likes",comment.getLikes());
            commentJsonObject.put("commentContent",comment.getContent());
//            commentJsonObject.put("replies",replyJsonArray);
            commentJsonObject.put("avatarImgUrl","/static/static/images/Logo_40.png");
//            commentJsonObject.put("location",comment.getLocation());
                commentJsonObject.put("master",1);

            commentJsonArray.add(commentJsonObject);
        }
        System.out.println(commentJsonArray);

        return BlogJSONResult.ok(commentJsonArray);




    }

    @Override
    public BlogJSONResult insertComment(Comment comment) {
        commentMapper.insertComment(comment);
        return BlogJSONResult.ok();
    }
}
